﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using OVS.CoreDomain;
using OVS.CoreDomain.Business;
using OVS.CoreDomain.Data;
using OVS.Data;


namespace OVS.Business
{
    public class BusinessCandidate : IBusinessCandidate
    {
        private readonly IDalCandidate _dalCandidate;
        private readonly IBusinessEncryptVotes _businessEncryptVotes;


        public BusinessCandidate (IDalCandidate dalCandidate)
        {
            _dalCandidate = dalCandidate;
        }

        public BusinessCandidate()
        {
            _dalCandidate = new DalCandidate();
            _businessEncryptVotes = new BusinessEncryptVotes();
        }

        public BusinessCandidate(IDalCandidate dalCandidate, IBusinessEncryptVotes businessEncryptVotes)
        {
            _dalCandidate = dalCandidate;
            _businessEncryptVotes = businessEncryptVotes;
        } 

     
        #region IBusinessCandidate Members

        public ICandidate GetCandidate(string voterId, IConstituency constituency)
        {
            return _dalCandidate.GetCandidate(voterId, constituency);
        }

        public bool AddCandidate(ICandidate candidate)
        {
           return  _dalCandidate.AddCandidate(candidate);
        }

        public bool UpdateCandidate(ICandidate candidate)
        {
            return _dalCandidate.UpdateCandidate(candidate);
        }

        #endregion

        #region IBusinessCandidate Members


        public IList<ICandidate> SearchCandidates(ICandidateSearchCriteria candidateSearchCriteria)
        {
            return _dalCandidate.SearchCandidates(candidateSearchCriteria);
        }

        #endregion

        #region IBusinessCandidate Members


        public ICandidate GetCandidate(int candidateId)
        {
            return _dalCandidate.GetCandidate(candidateId);
        }

        #endregion

        #region IBusinessCandidate Members


        public List<ICandidate> GetCandidates(IConstituency constituency)
        {
            return _dalCandidate.GetCandidates(constituency);
        }

        #endregion



        #region IBusinessCandidate Members


        public ICandidate GetVotes(ICandidate candidate)
        {
            return _dalCandidate.GetVote(candidate);
        }

        private ICandidate IncrementVote(ICandidate candidate)
        {
            ICandidate _candidate = this.GetVotes(candidate);
            _businessEncryptVotes.DecryptVote(_candidate);
            _candidate.Votes++;
            _businessEncryptVotes.EncryptVote(_candidate);
            return _candidate;
        }

        public bool UpdateVote(ICandidate candidate, IVoter voter)
        {
            ICandidate _candidate = this.GetVotes(candidate);
            _candidate = this.IncrementVote(_candidate);
            return _dalCandidate.UpdateVote(_candidate,voter);
        }

        #endregion

      
    }
}
